#include "saveExpInfo.h"

SaveExpInfo::SaveExpInfo()
{

}

void SaveExpInfo::calculate(QString equId){

    LogHelperJ::write("Start Save Exp Info =============");

    if(pState != nullptr){
        delete pState;
    }
    pState = new PCRState();

    LogHelperJ::write("------> create pState succeed");
    pState->init(equId);
    qDebug() << "--------> 数据计算结束";
    LogHelperJ::write("------> dataAnalysis succeed");
    exportReport(equId);
    qDebug() << "--------> 导出报告结束";
    LogHelperJ::write("------> export report succeed");
    saveHistory(equId);
    qDebug() << "--------> 数据库存取结束";
    LogHelperJ::write("------> database update succeed");

    if(pState != nullptr){
        delete pState;
        pState = nullptr;
    }
    LogHelperJ::write("End Save Exp Info =============");

}

void SaveExpInfo::exportReport(QString equId){

    QString outdir;
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select Name from ItemCode Where DicCode=100");
    query.exec(sql);
    while(query.next()){
        outdir = query.value("Name").toString();
    }
    if(outdir == ""){
        qDebug() << "Error: 自动导出路径为空";
        return;
    }
    QDir dir;
    if (!dir.exists(outdir)) {
        dir.mkpath(outdir); // 创建目录，包括所有必要的上级目录
    }
    pState->datao(outdir + "/" + QDateTime::currentDateTime().toString("yyyy-MM-dd_HH-mm-ss") + ".xlsx");

}

void SaveExpInfo::saveHistory(QString equId){


    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql;


    // 实验Id
    QString expId;
    sql = QString("select experimentguid from equipmentinfo where id='%1'").arg(equId);
    query.exec(sql);
    while(query.next()){
        expId = query.value("experimentGuid").toString();
    }
    if(expId == ""){
        qDebug() << "Error: 未找到实验Id";
        return;
    }

    db.transaction();
    foreach(POSI posi, pState->posiList.keys()){
        foreach(CHANNEL channel, pState->channelList.keys()){
            CURVE curve = posi + 100*channel;
            QString tempId = QUuid::createUuid().toString(QUuid::Id128);
            sql = QString("Insert into HistoryExperimentDetails(ID, ExperimentID, Posi, SampleName, Channel, Type, CT, AvgCT, Conc, Efficiency, R2,   Y,  Slope,  AvgConc) Values(  '%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8', '%9', '%10', '%11', '%12', '%13', '%14')")
                            .arg(tempId, expId, getPosiName(posi),
                                 pState->samplePannel[posi].name,
                                 PCRState::CHANNELLIST[channel],
                                 pState->samplePannel[posi].type,
                                 pState->Ct.contains(curve)             ? QString::number(pState->Ct[curve])                        : "0",
                                 pState->avgCt.contains(channel)        ? QString::number(pState->avgCt[channel])                   : "0",
                                 pState->concList.contains(curve)       ? QString::number(pState->concList[curve])                  : "0",
                                 pState->E.contains(channel)            ? QString::number(pState->E[channel])                       : "0",
                                 "0.99",
                                 pState->standardLine.contains(channel) ? QString::number(pState->standardLine[channel][1])         : "0",
                                 pState->standardLine.contains(channel) ? QString::number(pState->standardLine[channel][0])         : "0",
                                 pState->avgConcList.contains(channel)  ? QString::number(pState->avgConcList[channel])             : "0");

            if(!query.exec(sql)){
                qDebug() << "Error: 历史数据插入错误";
            }
//            qDebug() << sql;
        }
    }
    if(!db.commit()){
        qDebug() << "Error: 历史数据Commit失败";
    }

}


